#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>

int sm3_hmac(const unsigned char *key, size_t key_len,
             const unsigned char *data, size_t data_len,
             unsigned char *out) {
    const EVP_MD *md = EVP_sm3();
    if (!md) return -1;
    if (!HMAC(md, key, key_len, data, data_len, out, NULL))
        return -1;
    return 0;
}

int main() {
    const char *msg = "fsl";
    const char *key_hex = "659366720CF0B1390D77D60202515C74";
    unsigned char key[16];
    unsigned char out[32]; // SM3 digest size

    for (int i = 0; i < 16; i++) {
        sscanf(key_hex + i * 2, "%2hhx", &key[i]);
    }

    if (sm3_hmac(key, 16, (unsigned char*)msg, strlen(msg), out) == 0) {
        for (int i = 0; i < 32; i++) {
            printf("%02x", out[i]);
        }
        printf("\n");
    }
    return 0;
}
